import { ArticleAPI } from "@/api/ArticleAPI";
import type { Active, Cover_tags } from "@/types/Active";
import type { CommentApi } from "@/types/Comment";
import type { Status } from "@/types/Status";
import { defineStore } from "pinia";
import { Formattime } from "@/logics/geshiitem";
import type { search_data } from "@/types/Search";

type State = {
   //文章详情
   article:{
    result:Partial<Active>;
    status:Status
   }
   //文章评论++
   comments:{
    result:CommentApi[],
    status:Status
   },
   Cover_tags:{
     result:Cover_tags,
     status:Status
   },
   //文章搜索数据
   search_article:{
    result:Active[]
    status:Status,
    count:number,
   }
};

type Getters = {};

type Actions = {
    //获取文章详情数据
    getArticle(nid:string):Promise<void>;
    //获取文章评论数据
    getArticle_comment(nid:string):Promise<void>;
    //获取文章评论数据
    getArticle_tags():Promise<void>;
    //获取文章搜索结果数据
    getsearch_Article(data:search_data):Promise<void>;
};

export const useArticleStore = defineStore<"article", State, Getters, Actions>(
  "article",
  {
    state: () => ({
      article:{
        result:{},
        status:'idle',
      
      },
      comments:{
        result:[],
        status:'idle'
       },
       Cover_tags:{
        result:{
          cover_List:[],
          tags_List:[]
        },
        status:'idle'
      },
      search_article:{
        result:[],
        status:'idle',
        count:0,
      }
    }
     ),
     
    actions:{
           //获取文章详情数据
        async getArticle(nid){
          //更新数据加载状态
          this.article.status = 'loading'
          //捕获错误
          try{
           //发送请求获取数据
           const response = await ArticleAPI.getArticle(nid)
           this.article.result = response.data
            // 更新数据加载状态
          this.article.status = "success";
          }catch(error){
             // 更新数据加载状态
             this.article.status = "error";
          }
        },   
        //获取文章评论数据
        async getArticle_comment(nid){
            //更新数据加载状态
            this.comments.status = 'loading'
            //捕获错误
            try{
             //发送请求获取数据
             const response = await ArticleAPI.getArticle_comment(nid)
             response.data.map(item=>{
              item.createTime=Formattime(item.createTime)
              item.reply?.list.map(sub=>sub.createTime=Formattime(sub.createTime))
             })
              // 存储文章评论数据
              // console.log(response.data);
             this.comments.result = response.data
              // 更新数据加载状态
            this.comments.status = "success";
            }catch(error){
               // 更新数据加载状态
               this.comments.status = "error";
            }
        },  
        //获取文章所有标签列表
        async getArticle_tags(){
          //更新数据加载状态
          this.Cover_tags.status = 'loading'
        //捕获错误
          try{
             //发送请求获取数据
           const response = await ArticleAPI.getArticle_tags()
           this.Cover_tags.result = response.data
        }catch(error){
             // 更新数据加载状态
             this.Cover_tags.status = "error";
        }
         },
        //获取文章搜索数据
        async getsearch_Article(data){
          //更新数据为加载状态
          this.search_article.status = "loading"
          //捕获错误
          try{
            //发送请求获取数据
            const response = await ArticleAPI.searchArticle(data)
            //保存数据
            this.search_article.result = response.data
            this.search_article.count =response.count
            //更新状态
            this.search_article.status = "success"
          }catch(error){
            //更新状态
            this.search_article.status = "error"
          }
        }
    }
  }
);